<div class="layui-fluid">
	<form class="layui-form" action="" lay-filter="goods-list-form" id="goods-list-form">
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<input type="text" name="no" id="no" class="layui-input" placeholder="请输入订单号">
			</div>
		</div>
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<input type="text" name="start_time" class="layui-input" id="test1" placeholder="下单开始时间">
			</div>
		</div>
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<input type="text" name="end_time" class="layui-input" id="test2" placeholder="下单结束时间">
			</div>
		</div>
		<!--<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<select name="category_pid" lay-verify="category_pid" lay-filter="category_pid" id="category_pid">
					<option value="">请选择订单类型</option>
				</select>
			</div>
		</div>-->
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<select name="pay_type" lay-verify="pay_type" lay-filter="pay_type" id="pay_type">
					<option value="">请选择支付方式</option>
					<option value="0">全部支付方式</option>
					<option value="1">余额支付方式</option>
					<option value="2">微信支付方式</option>
				</select>
			</div>
		</div>
		<!--<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<select name="state" lay-verify="state" lay-filter="state" id="state">
					<option value="">请选择订单状态</option>
					<option value="0">全部订单状态</option>
					<option value="1">待发货</option>
					<option value="2">已发货</option>
					<option value="3">已完成</option>
					<option value="-1">已退款</option>
				</select>
			</div>
		</div>-->
		<!--<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<input type="text" name="no" id="no" class="layui-input" placeholder="请输入书的名称">
			</div>
		</div>-->
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<input type="text" name="nickname" id="nickname" class="layui-input" placeholder="请输入昵称或订单的手机号">
			</div>
		</div>
		<div class="layui-form-item layui-inline" style="margin-left:20px;">
			<div class="layui-input-inline">
				<button class="layui-btn" lay-submit lay-filter="goods-list-search">搜索</button>
				<!-- <button class="layui-btn layui-btn-primary" lay-filter="goods-list-search1">重置</button>-->
			</div>
		</div>
	</form>

	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body">
					<table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>
					<script type="text/html" id="user_avatar">
						<img src="{{d.user_avatar}}" alt="" style="width:80px; height: 80px;border-radius: 50%;">
					</script>
					<script type="text/html" id="LAY-good-list-table-stock">
						<p>￥{{d.price}}</p>
					</script>
					<script type="text/html" id="LAY-good-list-table-stock1">
						<p>{{parseInt(d.state) ==1? '已付款，待平台发货': parseInt(d.state) ==2?'平台已发货': parseInt(d.state) ==3?'已签收，订单完成': '订单取消，已退款'}}</p>
					</script>
					<script type="text/html" id="LAY-good-list-table-stock4">
						<p>{{parseInt(d.pay_type) ==2? '微信支付': '余额支付'}}</p>
					</script>
					<script type="text/html" id="LAY-good-list-table-stock2">
						<p>{{d.amount}}本</p>
					</script>
                    <script type="text/html" id="user_name">
						<a lay-event="detail" style="cursor: pointer;color:blue">{{d.user_nickname}}</a>
					</script>
					<script type="text/html" id="test-table-operate-barDemo">
						<a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>
					</script>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	layui.use(['admin', 'table', 'form', 'upload', 'laydate'], function() {
		var user_id = layui.router().search.user_id
		var table = layui.table,
			laydate = layui.laydate,
			form = layui.form,
			admin = layui.admin,
			upload = layui.upload,
			view = layui.view,
			element = layui.element,
			$ = layui.$,
			cols = [
				[{
						align: 'center',
						title: '订单号',
						width: 166,
						field: 'no'
					},
					{
						align: 'center',
						field: 'amount',
						title: '商品数量',
						templet: '#LAY-good-list-table-stock2'
					},
					{
						align: 'center',
						width: 130,
						templet: '#user_avatar',
						title: '买家用户头像',
						field: 'user_avatar'
					}, {
						align: 'center',
						field: 'user_nickname',
						title: '买家用户',
						templet: '#user_name'
					}, {
						align: 'center',
						field: 'created_at',
						title: '下单时间'
					}, {
						align: 'center',
						field: 'state',
						title: '订单状态',
						templet: '#LAY-good-list-table-stock1'
					},
					{
						align: 'center',
						field: 'pay_type',
						title: '支付方式',
						templet: '#LAY-good-list-table-stock4'
					},
					{
						align: 'center',
						field: 'price',
						title: '支付金额',
						templet: '#LAY-good-list-table-stock'
					},
					{
						align: 'center',
						fixed: 'right',
						title: '操作',
						toolbar: '#test-table-operate-barDemo'
					}
				]
			];
		laydate.render({
			elem: '#test1',
			type: 'datetime'
		});
		laydate.render({
			elem: '#test2',
			type: 'datetime'
		});
		table.set({
			headers: { //通过 request 头传递
				'access-token': layui.data('layuiAdmin').access_token
			}
		});
		form.render(null, 'goods-list-form');
		table.render({
			elem: '#test-table-operate',
			url: 'https://bookshop.dingdangtoy.com/api/admin/order/index?state=-1&user_id=' + user_id,
			parseData: function(res) { //res 即为原始返回的数据
				return {
					"code": res.code, //解析接口状态
					"msg": res.message, //解析提示文本
					"data": res.data.items, //解析数据列表
					"count": res.data._meta.total_count //解析数据长度
				}
			},
			cols: cols,
			page: true
		});

		//监听工具条
		table.on('tool(test-table-operate)', function(obj) {
			var data = obj.data;
			var oder_id = obj.data.id;
			if(obj.event === 'edit') {
				admin.popup({
					title: '订单详情',
					area: ['96%', '96%'],
					id: 'LAY-popup-content-tags',
					success: function(layero, index) {
						view(this.id).render('sale-orders/all/add', data).done(function() {
							if(parseInt(data.state) == -1) {
								$('#speed-progress-2').show();
							} else {
								$('#speed-progress-1').show();
							}
							if(parseInt(data.state) == 1) {
								$('#ppx1').addClass('active')
								$('#ppx2').addClass('active')
								$('#div3').show();
							} else if (parseInt(data.state) == 2) {
								$('#ppx2').addClass('active')
								$('#div4').show();
							} else if (parseInt(data.state) == 3) {
								$('#div5').show();
							}else {
								$('#div6').show();
							}
							// 商品table
							var cols1 = [
								[{
										align: 'center',
										field: 'book_image',
										title: '商品图片',
										width: 110,
										templet: '#LAY-good-list-table-stock5'
									},
									{
										align: 'center',
										title: '商品名称',
										field: 'book_title'
									},
									{
										align: 'center',
										title: '作者',
										field: 'book_author'
									},
									{
										align: 'center',
										title: 'ISBN',
										width: 166,
										field: 'isbn'
									},
									{
										align: 'center',
										title: '原价',
										width: 166,
										field: 'book_old_price',
										templet: '#LAY-good-list-old-price'
									},
									{
										align: 'center',
										title: '剩余库存',
										field: 'book_total_stock'
									},
									{
										align: 'center',
										title: '一级分类',
										field: 'book_parent_category'
									},
									{
										align: 'center',
										title: '二级分类',
										field: 'book_categories',
										templet: '#LAY-good-list-table-stock7'
									},
									{
										align: 'center',
										title: '选择的品相',
										field: 'type',
										templet: '#LAY-good-list-table-stock8'
									},
									{
										align: 'center',
										title: '价格',
										field: 'price',
										templet: '#LAY-good-list-table-stock6'
									}
								]
							];
							table.set({
								headers: { //通过 request 头传递
									'access-token': layui.data('layuiAdmin').access_token
								}
							});
							table.render({
								elem: '#LAY-good-list-status1',
								url: 'https://bookshop.dingdangtoy.com/api/admin/order/show?order_id=' + data.id,
								parseData: function(res) { //res 即为原始返回的数据
									return {
										"code": res.code, //解析接口状态
										"msg": res.message, //解析提示文本
										"data": res.data.books, //解析数据列表
									}
								},
								cols: cols1,
								page: false
							});
							// 主动退款
							$('#detection1').on('click', function() {
								//执行 Ajax 后重载
								$.ajax({
									type: "POST",
									dataType: "json",
									url: "https://bookshop.dingdangtoy.com/api/admin/order/refund",
									data: {
										order_id: data.id
									},
									headers: { //通过 request 头传递
										'access-token': layui.data('layuiAdmin').access_token
									},
									success: function(data) {
										if(data.code == 0) {
                                            layer.msg('操作成功');
                                            layui.table.reload('test-table-operate');
										} else {
											layer.msg(data.message);
										}
									},
									error: function(msg) {
										console.log(msg)
									}
								});
								return false;
							});
							// 点击预约发货
							$('#detection').on('click', function() {
								//执行 Ajax 后重载
								$.ajax({
									type: "POST",
									dataType: "json",
									url: "https://bookshop.dingdangtoy.com/api/admin/order/send",
									data: {
										order_id: data.id
									},
									headers: { //通过 request 头传递
										'access-token': layui.data('layuiAdmin').access_token
									},
									success: function(data) {
										if(data.code == 0) {
                                            var express_waybill_id = data.data.express_waybill_id
                                            var delivery_id = data.data.delivery_id
                                                  layer.open({
											        type: 1
											        ,title: '已生成运单号，是否确认发货'
											        ,area: '300px;'
											        ,shade: 0.8
											        ,id: 'LAY_layuipro'
											        ,btn: ['确认发货']
											        ,btnAlign: 'c'
											        ,moveType: 1 //拖拽模式，0或者1
											        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #fff; color: #000; font-weight: 300;"><p>快递公司：'+delivery_id+'</p><p>快递单号：'+express_waybill_id+'</p></div>'
											        ,success: function(layero){
											         // 点击发货
											          var btn = layero.find('.layui-layer-btn');
											          btn.find('.layui-layer-btn0').click(function(){
														$.ajax({
															type: "POST",
															dataType: "json",
															url: "https://bookshop.dingdangtoy.com/api/admin/order/express",
															data: {
																order_id: oder_id
															},
															headers: { //通过 request 头传递
																'access-token': layui.data('layuiAdmin').access_token
															},
															success: function(data) {
																if(data.code == 0) {
						                                            layer.msg('操作成功');
						                                            layui.table.reload('test-table-operate');
																} else {
																	layer.msg(data.message);
																}
															},
															error: function(msg) {
																console.log(msg)
															}
														});
											          })
											        }
											      });
										} else {
											layer.msg(data.message);
										}
									},
									error: function(msg) {
										console.log(msg)
									}
								});
								return false;
							});
							$.ajax({
								type: "GET",
								dataType: "json",
								url: "https://bookshop.dingdangtoy.com/api/admin/order/show?order_id=" + data.id,
								data: {},
								headers: { //通过 request 头传递
									'access-token': layui.data('layuiAdmin').access_token
								},
								success: function(data) {
									if(parseInt(data.code) == 0) {
										$('#no1').text(data.data.no)
										$('#address_receiver').text(data.data.address_receiver)
										$('#address_mobile').text(data.data.address_mobile)
										$('#address_province_name').text(data.data.address_province_name)
										$('#address_city_name').text(data.data.address_city_name)
										$('#address_area_name').text(data.data.address_area_name)
										$('#address').text(data.data.address)
										$('#send_at').text(data.data.send_at)
										$('#created_at1').text(data.data.pay_at)
										$('#pay_at').text(data.data.pay_at)
										$('#amount1').text(data.data.amount)
										$('#done_at').text(data.data.done_at)
										$('#fee_price').text(data.data.fee_price)
										$('#express_delivery_id').text(data.data.express_delivery_id)
										$('#express_no').text(data.data.express_no)
										$('#cancel_at').text(data.data.cancel_at)
										$('#user_nickname').text(data.data.user_nickname)
										$('#user_avatar1').attr('src', data.data.user_avatar)
										if(parseFloat(data.data.coupon_decr_price)>0){
											$('#px2').show();
											$('#coupon_decr_price').text(data.data.coupon_decr_price)
										}
										$('#book_price').text(data.data.book_price)
										$('#price').text(data.data.price)
										$('#express_no1').text(data.data.express_no)
										$('#express_no2').text(data.data.express_no)
										if (data.data.express_delivery_id == 'ZTO') {
											$('#express_delivery_id2').text('中通快递');
										}else{
											$('#express_delivery_id2').text('');
										}
										if (data.data.express_delivery_id == 'ZTO') {
											$('#express_delivery_id1').text('中通快递');
										}else{
											$('#express_delivery_id1').text('');
										}
										if(parseInt(data.data.pay_type) == 1) {
											$('#pay_type1').text('余额支付');
											$('#px1').show();
											$('#account_decr_price').text(data.data.account_decr_price)
										} else {
											$('#pay_type1').text('微信支付');
										}
										if (data.data.express_info.length == 0) {
											$('#logistics_d').show()
										}
										var _html2 = '';
										console.log(data.data.express_info)
                                        for(var i=0;i<data.data.express_info.length;i++){
                                        	_html2+= '<div class="logistics-1"><p>'+data.data.express_info[i].action_time+'</p><p>'+data.data.express_info[i].action_msg+'</p></div>'
                                        }
                                        $('#logistics').append(_html2)
                                        $('#logistics1').append(_html2)
										form.render();
									} else {
										layer.msg(data.message);
									}
								},
								error: function(msg) {
									console.log(msg)
								}
							});

						});
					}
				});
			} else if (obj.event === 'detail') {
				admin.popup({
					title: '查看用户',
					area: ['80%', '96%'],
					id: 'LAY-popup-content-tags',
					success: function(layero, index) {
						view(this.id).render('user-manage/all/add', data).done(function() {
							var is_black = '';
							var titles = '';
							var contents = '';
							// 跳转收书列表
							$('#order_books1').click(function() {
								location.hash = '/buy-orders/all/index/user_id=' + data.user_id
								return false;
							})
							// 跳转卖书列表
							$('#order_books').click(function() {
								location.hash = '/sale-orders/all/index/user_id=' + data.user_id
								return false;
							})
							// 跳转订单退回列表
							$('#order_books2').click(function() {
								location.hash = '/return-order/order/index/user_id=' + data.user_id
								return false;
							})
							$.ajax({
								type: "GET",
								dataType: "json",
								url: "https://bookshop.dingdangtoy.com/api/admin/user/show?id=" + data.user_id,
								data: {},
								headers: { //通过 request 头传递
									'access-token': layui.data('layuiAdmin').access_token
								},
								success: function(data) {
									if(parseInt(data.code) == 0) {
										$('#wechat_avatar1').attr('src', data.data.wechat_avatar)
										$('#wechat_nickname').text(data.data.wechat_nickname)
										$('#account').text(data.data.account)
										$('#created_at').text(data.data.created_at)
										$('#recommend_users_success').text(data.data.recommend_users_success)
										$('#buy_orders_num').text(data.data.buy_orders_num)
										$('#buy_books_num').text(data.data.buy_books_num)
										$('#sell_orders_num').text(data.data.sell_orders_num)
										$('#sell_books_num1').text(data.data.sell_books_num1)
										$('#sell_books_num2').text(data.data.sell_books_num2)
										$('#back_orders_num').text(data.data.back_orders_num)
										$('#back_books_num').text(data.data.back_books_num)
										$('#get_money').text(data.data.get_money)

										if(parseInt(data.data.is_official_account) == 1) {
											$('#wechat_nickname1').text('已关注公众号')
										} else {
											$('#wechat_nickname1').text('未关注公众号')
										}
										if(parseInt(data.data.is_black) == 1) {
											is_black = 0;
											$('#blacklist').text('取消标记黑名单');
											$('#blacklistUser').show();
											titles = '确认将用户取消标记';
											contents = '确认后，该用户的卖书权限将恢复，是否确认操作';
										} else {
											is_black = 1;
											$('#blacklist').text('标记为黑名单用户');
											titles = '确认将用户标记为黑名单';
											contents = '提交后，该用户卖书下单时将会被系统自动拒绝是否确认操作？';
										}
										$('#recommend_users_total').text(data.data.recommend_users_total)
										if(data.data.addresses.length > 0) {
											$('#base-address').show();
										}
										var _html = '';
										var _html1 = '';
										var _html2 = '';
										var num = 0;
										for(var i = 0; i < data.data.addresses.length; i++) {
											num += 1
											_html += '<li><p>地址信息' + num + '</p><p><span style="margin-right:20px">' + data.data.addresses[i].receiver + '</span>' + data.data.addresses[i].mobile + '</p><p>' + data.data.addresses[i].province_name + '' + data.data.addresses[i].city_name + '' + data.data.addresses[i].area_name + '' + data.data.addresses[i].address + '</p></li>';
										}
										$('#address-list').append(_html)
										for(var i = 0; i < data.data.account_records1.length; i++) {
											_html1 += '<tr><td>卖书（' + data.data.account_records1[i].books_num + '本）</td><td>' + data.data.account_records1[i].created_at + '</td><td>' + data.data.account_records1[i].platform_success_time + '</td><td>' + data.data.account_records1[i].actual_price + '</td></tr>';
										}
										$('#table1').append(_html1)
										for(var i = 0; i < data.data.account_records2.length; i++) {
											if(parseInt(data.data.account_records2[i].state) == 1) {
												_html2 += '<tr><td>' + data.data.account_records2[i].remark + '</td><td>' + data.data.account_records2[i].created_at + '</td><td>' + data.data.account_records2[i].account + '</td><td>提现成功</td></tr>';
											} else {
												_html2 += '<tr><td>' + data.data.account_records2[i].remark + '</td><td>' + data.data.account_records2[i].created_at + '</td><td>' + data.data.account_records2[i].account + '</td><td stle="color:red">提现失败</td></tr>';

											}
										}
										$('#table2').append(_html2)

										form.render();
									} else {
										layer.msg(data.message);
									}
								},
								error: function(msg) {
									console.log(msg)
								}
							});
              // 黑名单加入与取消
							$('#blacklist').click(function() {
									//执行 Ajax 后重载
									layer.open({
							        type: 1
							        ,title: titles
							        ,area: '300px;'
							        ,shade: 0.8
							        ,id: 'LAY_layuipro'
							        ,btn: ['确认','取消',]
							        ,btnAlign: 'c'
							        ,moveType: 1 //拖拽模式，0或者1
							        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #fff; color: #000; font-weight: 300;"><p>'+contents+'</p></div>'
							        ,success: function(layero){
							          var btn = layero.find('.layui-layer-btn');
							          btn.find('.layui-layer-btn0').click(function(){
													$.ajax({
															type: "POST",
															dataType: "json",
															url: "https://bookshop.dingdangtoy.com/api/admin/user/black",
															data: {
																'user_id':data.user_id,
																'is_black':is_black,
															},
															headers: {
																		'access-token': layui.data('layuiAdmin').access_token
																	},
														success: function(data){
															if(data.code == 0){
																layer.msg('操作成功');
																layui.table.reload('test-table-operate');
																setTimeout(function(){
																		window.parent.layer.closeAll();
																},1000)
															} else {
																layer.msg(data.message);
															}

														},
														error: function(msg){
															console.log(msg)
														}
													});
							          })
							        }
							      });
								return false;


							})
						});
					}
				});
			}
		});
		// 搜索 ,

		form.on('submit(goods-list-search)', function(data) {
			table.reload('test-table-operate', {
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
					'no': $('#no').val(),
					'start_time': $('#test1').val(),
					'end_time': $('#test2').val(),
					'pay_type': $('#pay_type option:selected').val(),
					'nickname': $('#nickname').val()
				}
			});
			return false;
		});

	});
</script>
<style>
	.layui-table-cell, .layui-table-tool-panel li{white-space: inherit !important;}
</style>
